package com.zs.zcerp.approval.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.zcerp.approval.entity.EmpEntity;
import com.zs.zcerp.approval.entity.QhBasestdDEntity;
import com.zs.zcerp.approval.entity.QhBasestdMEntity;
import com.zs.zcerp.approval.entity.QhChkmgrdEntity;
import com.zs.zcerp.approval.mapper.QhBasestdDMapper;
import com.zs.zcerp.approval.service.IQhBasestdDService;
import com.zs.zcerp.approval.service.IQhChkmgrdService;
import com.zs.zcerp.common.entity.FebsConstant;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.common.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * Henry
 */

@Service
@RequiredArgsConstructor
@DS("oracle") //从oracle库数据查询
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class QhBasestdDServiceImpl extends ServiceImpl<QhBasestdDMapper, QhBasestdDEntity> implements IQhBasestdDService {

    private final IQhChkmgrdService qhChkmgrdService;

    @Override
    public IPage<QhBasestdDEntity> findClassifyDetailList(QhBasestdDEntity qhBasestdDEntity, QueryRequest request) {
        Page<QhBasestdDEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countClassifyDetail(qhBasestdDEntity));
        SortUtil.handlePageSort(request, page, "basestdNo", FebsConstant.ORDER_ASC, false);
        return baseMapper.findClassifyDetailPage(page, qhBasestdDEntity);
    }



    @Override
    public void createBaseList(List<QhBasestdDEntity>  qhBasestdDEntityList) {
        saveBatch(qhBasestdDEntityList);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bacthUpdate(List<QhBasestdDEntity> qhBasestdDEntityList) {
        for (QhBasestdDEntity q : qhBasestdDEntityList){
            // 新建需要修改的对象的值
            QhChkmgrdEntity qhChkmgrdEntity = new QhChkmgrdEntity();
            qhChkmgrdEntity.setToUserCname(q.getNameS());
            qhChkmgrdEntity.setToUser(q.getChkUser());
            qhChkmgrdEntity.setNextUser(q.getChkUser());
            qhChkmgrdEntity.setFlowDesc(q.getNameT());

            if(q.getToMail()!=null){
                qhChkmgrdEntity.setToMail(q.getToMail());
            }

            // 更新QH_BASESTD_D表中的对象值  旧版本只是修改单个流程
            baseMapper.update(q, new QueryWrapper<QhBasestdDEntity>().eq("BASESTD_NO", q.getBasestdNo()).eq("ORG_ID", q.getOrgId()).eq("FLOW_SEQ", q.getFlowSeq()));
            //获取流程相关的单号 修改 跟换签核人
            List<String> orderList = qhChkmgrdService.getOrderList(q);

            // 批量跟换签核人 所有流程里面的账号
            //baseMapper.updateChkUser(q);
            // 更新QH_CHKMGR_D表中的对象值   批量跟换签核人 所有订单
             if(q.getOldChkUser()!=null&& q.getOldNameS()!=null){
                 //qhChkmgrdService.updateByToUser(qhChkmgrdEntity, q.getOldChkUser());
                //qhChkmgrdService.upadteNextUser(q.getOldChkUser(), q.getChkUser());
                 qhChkmgrdService.updateToUserAndNextUser(qhChkmgrdEntity,orderList);
             }

              /*//下一个步骤不用设置状态
             else{
                 qhChkmgrdService.updateByToUser(qhChkmgrdEntity, q.getChkUser());
                 qhChkmgrdService.upadteNextUser(q.getChkUser(), q.getChkUser());
             }*/


        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cruUpdate(List<QhBasestdDEntity> qhBasestdDEntityList) {
        for (QhBasestdDEntity q : qhBasestdDEntityList){
            // 新建需要修改的对象的值
            QhChkmgrdEntity qhChkmgrdEntity = new QhChkmgrdEntity();
            qhChkmgrdEntity.setToUserCname(q.getNameS());
            qhChkmgrdEntity.setToUser(q.getChkUser());
            qhChkmgrdEntity.setFlowDesc(q.getNameT());
            if(q.getToMail()!=null){
                qhChkmgrdEntity.setToMail(q.getToMail());
            }
            // 更新QH_CHKMGR_D表中的对象值  修改签核人员历史数据
            if(q.getOldChkUser()!=null&& q.getOldNameS()!=null){
                qhChkmgrdService.updateByToUser(qhChkmgrdEntity, q.getOldChkUser());
                    qhChkmgrdService.upadteNextUser(q.getOldChkUser(), q.getChkUser());
            }

        }
    }



    @Override
    public void bacthDelete(List<QhBasestdDEntity> qhBasestdDEntityList) {
      for (QhBasestdDEntity q : qhBasestdDEntityList) {
          baseMapper.delete(new QueryWrapper<QhBasestdDEntity>().eq("BASESTD_NO", q.getBasestdNo()).eq("ORG_ID", q.getOrgId()).eq("FLOW_SEQ", q.getFlowSeq()));
      }
    }

    @Override
    public void deleteByorgIdAndBasestdNo(Integer orgId, String basestdNo) {
        baseMapper.delete(new QueryWrapper<QhBasestdDEntity>().eq("BASESTD_NO", basestdNo).eq("ORG_ID", orgId));
    }



    @Override
    public void deleteBasestdMByBasestdNo(String[] basestdNos) {
        List<String> list = Arrays.asList(basestdNos);
        baseMapper.delete(new QueryWrapper<QhBasestdDEntity>().lambda().in(QhBasestdDEntity::getBasestdNo, list));
    }

    @Override
    public List<QhBasestdDEntity> findbaseStdDetails(String basestdNo,String orgId) {
        return  baseMapper.findbaseStdDetails(basestdNo,orgId);
    }

    @Override
    public void bacthUpdateNames() {
        List<EmpEntity> list = this.findEmpNoAndNames();
        for (EmpEntity q : list) {
           this.updateNamesbychkUser(q);
            this.updateToUserCnamebychkUser(q);
            this.updateGrtUserCname(q);
        }

    }

    /**
     * 获取用户工号 和名字
     * @return
     */
    @Override
    public List<EmpEntity> findEmpNoAndNames() {
        return baseMapper.findEmpNoAndNames();
    }



    @Override
    public void updateNamesbychkUser(EmpEntity q) {
        baseMapper.updateNamesbychkUser(q);
    }

    public void updateToUserCnamebychkUser(EmpEntity q) {
        baseMapper.updateToUserCnamebychkUser(q);
    }
    public void updateGrtUserCname(EmpEntity q) {
        baseMapper.updateGrtUserCname(q);
    }

}
